<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[etcd] etcd集群配置与使用 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="etcd集群配置与使用 一.etcd原理 1.etcd介绍 etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统，可以" /><meta name="keywords" content="中间件, middleware, linux, centos7, etcd" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/middleware/etcd%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[etcd] etcd集群配置与使用" />
<meta property="og:description" content="etcd集群配置与使用 一.etcd原理 1.etcd介绍 etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统，可以" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/middleware/etcd%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-12-25T15:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[etcd] etcd集群配置与使用">
<meta itemprop="description" content="etcd集群配置与使用 一.etcd原理 1.etcd介绍 etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统，可以"><meta itemprop="datePublished" content="2021-12-25T15:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="1823">
<meta itemprop="keywords" content="中间件,middleware,linux,centos7,etcd," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[etcd] etcd集群配置与使用"/>
<meta name="twitter:description" content="etcd集群配置与使用 一.etcd原理 1.etcd介绍 etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统，可以"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[etcd] etcd集群配置与使用</h1>

      <div class="post-meta">
        <span class="post-time"> 2021-12-25 </span>
        <div class="post-category">
            <a href="/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/"> 中间件 </a>
            </div>
          <span class="more-meta"> 约 1823 字 </span>
          <span class="more-meta"> 预计阅读 4 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#etcd集群配置与使用">etcd集群配置与使用</a>
      <ul>
        <li><a href="#一etcd原理">一.etcd原理</a>
          <ul>
            <li><a href="#1etcd介绍">1.etcd介绍</a></li>
            <li><a href="#2应用场景">2.应用场景</a></li>
          </ul>
        </li>
        <li><a href="#二etcd集群部署">二.etcd集群部署</a>
          <ul>
            <li><a href="#1主机规划">1.主机规划</a></li>
            <li><a href="#2设置3台主机的hostname">2.设置3台主机的hostname</a></li>
            <li><a href="#3安装etcd">3.安装etcd</a></li>
            <li><a href="#4查看安装的etcd的版本号">4.查看安装的etcd的版本号</a></li>
            <li><a href="#6启动服务">6.启动服务</a></li>
            <li><a href="#7配置防火墙开放端口">7.配置防火墙开放端口</a></li>
            <li><a href="#8集群维护">8.集群维护</a></li>
            <li><a href="#9etcd-api分为2和3版本">9.etcd api分为2和3版本</a></li>
            <li><a href="#10etcd操作数据">10.etcd操作数据</a></li>
            <li><a href="#11测试集群是否成功">11.测试集群是否成功</a></li>
          </ul>
        </li>
        <li><a href="#三应用中使用">三.应用中使用</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h1 id="etcd集群配置与使用">etcd集群配置与使用</h1>
<h2 id="一etcd原理">一.etcd原理</h2>
<h3 id="1etcd介绍">1.etcd介绍</h3>
<p>etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统，可以用于配置共享和服务的注册和发现。</p>
<p>类似项目有zookeeper和consul。</p>
<p>etcd具有以下特点：</p>
<ul>
<li>完全复制：集群中的每个节点都可以使用完整的存档</li>
<li>高可用性：Etcd可用于避免硬件的单点故障或网络问题</li>
<li>一致性：每次读取都会返回跨多主机的最新写入</li>
<li>简单：包括一个定义良好、面向用户的API（gRPC）</li>
<li>安全：实现了带有可选的客户端证书身份验证的自动化TLS</li>
<li>快速：每秒10000次写入的基准速度</li>
<li>可靠：使用Raft算法实现了强一致、高可用的服务存储目录</li>
</ul>
<p>etcd 作为一个高可用键值存储系统，天生就是为集群化而设计的。由于 Raft 算法在做决策时需要多数节点的投票，所以 etcd 一般部署集群推荐奇数个节点，推荐的数量为 3、5 或者 7 个节点构成一个集群</p>
<h3 id="2应用场景">2.应用场景</h3>
<ul>
<li>
<p>服务发现</p>
<blockquote>
<p>服务发现要解决的也是分布式系统中最常见的问题之一，即在同一个分布式集群中的进程或服务，要如何才能找到对方并建立连接。本质上来说，服务发现就是想要了解集群中是否有进程在监听 udp 或 tcp 端口，并且通过名字就可以查找和连接</p>
</blockquote>
</li>
<li>
<p>配置中心</p>
<blockquote>
<p>将一些配置信息放到 etcd 上进行集中管理。</p>
<p>这类场景的使用方式通常是这样：应用在启动的时候主动从 etcd 获取一次配置信息，同时，在 etcd 节点上注册一个 Watcher 并等待，以后每次配置有更新的时候，etcd 都会实时通知订阅者，以此达到获取最新配置信息的目的。</p>
</blockquote>
</li>
<li>
<p>分布式锁</p>
<blockquote>
<p>因为 etcd 使用 Raft 算法保持了数据的强一致性，某次操作存储到集群中的值必然是全局一致的，所以很容易实现分布式锁。锁服务有两种使用方式，一是保持独占，二是控制时序</p>
</blockquote>
</li>
</ul>
<h2 id="二etcd集群部署">二.etcd集群部署</h2>
<h3 id="1主机规划">1.主机规划</h3>
<table>
<thead>
<tr>
<th>主机名称</th>
<th>角色</th>
<th>数量</th>
<th>主机内网IP规划</th>
<th>主机外网IP</th>
<th>主机配置</th>
<th>基础软件</th>
<th>系统</th>
</tr>
</thead>
<tbody>
<tr>
<td>node1</td>
<td>主节点</td>
<td>1</td>
<td>10.0.8.116</td>
<td>xxx</td>
<td>8C 8G</td>
<td>wget &amp;&amp;yum install -y etcd</td>
<td>centos 7.9 3.10.0</td>
</tr>
<tr>
<td>node2</td>
<td>从节点</td>
<td>1</td>
<td>10.0.8.159</td>
<td>xxx</td>
<td>8C 8G</td>
<td>wget &amp;&amp;yum install -y etcd</td>
<td>centos 7.9 3.10.0</td>
</tr>
<tr>
<td>node3</td>
<td>从节点</td>
<td>1</td>
<td>10.0.8.122</td>
<td>xxx</td>
<td>8C 8G</td>
<td>wget &amp;&amp;yum install -y etcd</td>
<td>centos 7.9 3.10.0</td>
</tr>
</tbody>
</table>
<blockquote>
<p>PS:这里要注意 etcd并不是很吃硬件，如果业务量不是很大的话给2C 4G就够了，如果业务量比较大的话4G 8G-16G或更大的硬件配置，自己灵活掌握。安装的时候需要注意7.2版本坑比较多，跟etcd的版本存在兼容性的问题，自己安装的过程中容易翻车</p>
</blockquote>
<h3 id="2设置3台主机的hostname">2.设置3台主机的hostname</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#分别在三个节点执行</span>
hostnamectl set-hostname node01
hostnamectl set-hostname node02
hostnamectl set-hostname node03

<span class="c1">#设置三个节点的host</span>
<span class="o">[</span>root@node3 /etc/etcd<span class="o">]</span><span class="c1"># vim /etc/hosts</span>
10.0.8.116 node1
10.0.8.159 node2
10.0.8.122 node3
</code></pre></td></tr></table>
</div>
</div><h3 id="3安装etcd">3.安装etcd</h3>
<p>etcd集群三种方式 静态集群 动态集群 DNS集群,我们这里搭建的是静态集群</p>
<p>● Static适用于有固定IP的主机节点</p>
<p>● etcd Discovery适用于DHCP环境</p>
<p>● DNS Discovery依赖DNS SRV记录</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="c1">#三台机器上都需要安装</span>
yum install -y etcd
</code></pre></td></tr></table>
</div>
</div><h3 id="4查看安装的etcd的版本号">4.查看安装的etcd的版本号</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#查看etcd版本</span>
<span class="o">[</span>root@node1 /var/lib/etcd/default.etcd/member<span class="o">]</span><span class="c1"># etcd --version</span>
etcd Version: 3.3.11
Git SHA: 2cf9e51
Go Version: go1.10.3
Go OS/Arch: linux/amd64

<span class="c1">#查看etcdctl版本</span>
<span class="c1">#v2接口</span>
etcdctl --version
etcdctl -v
<span class="c1">#v3接口</span>
etcdctl version
</code></pre></td></tr></table>
</div>
</div><p>###5.修改配置文件</p>
<h4 id="51修改-libsystemdsystemetcdservice">5.1修改 /lib/systemd/system/etcd.service</h4>
<p>三个节点都需要修改,重点在usre=root</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="o">[</span>root@master lib<span class="o">]</span><span class="c1"># more /lib/systemd/system/etcd.service </span>
<span class="o">[</span>Unit<span class="o">]</span>
<span class="nv">Description</span><span class="o">=</span>Etcd Server
<span class="nv">After</span><span class="o">=</span>network.target
<span class="nv">After</span><span class="o">=</span>network-online.target
<span class="nv">Wants</span><span class="o">=</span>network-online.target

<span class="o">[</span>Service<span class="o">]</span>
<span class="nv">Type</span><span class="o">=</span>notify
<span class="nv">WorkingDirectory</span><span class="o">=</span>/var/lib/etcd/
<span class="nv">EnvironmentFile</span><span class="o">=</span>-/etc/etcd/etcd.conf
<span class="nv">User</span><span class="o">=</span>root
<span class="c1"># set GOMAXPROCS to number of processors</span>
<span class="nv">ExecStart</span><span class="o">=</span>/usr/bin/etcd  <span class="se">\
</span><span class="se"></span>    --name<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_NAME</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --data-dir<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_DATA_DIR</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --listen-client-urls<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_LISTEN_CLIENT_URLS</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --listen-peer-urls<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_LISTEN_PEER_URLS</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --advertise-client-urls<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_ADVERTISE_CLIENT_URLS</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --initial-cluster-token<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_INITIAL_CLUSTER_TOKEN</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --initial-advertise-peer-urls<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_INITIAL_ADVERTISE_PEER_URLS</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --initial-cluster-state<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_INITIAL_CLUSTER_STATE</span><span class="si">}</span> <span class="se">\
</span><span class="se"></span>    --initial-cluster<span class="o">=</span><span class="si">${</span><span class="nv">ETCD_INITIAL_CLUSTER</span><span class="si">}</span>


<span class="nv">Restart</span><span class="o">=</span>on-failure
<span class="nv">LimitNOFILE</span><span class="o">=</span><span class="m">65536</span>

<span class="o">[</span>Install<span class="o">]</span>
<span class="nv">WantedBy</span><span class="o">=</span>multi-user.target
</code></pre></td></tr></table>
</div>
</div><h4 id="52修改etcetcdetcdconf">5.2修改/etc/etcd/etcd.conf</h4>
<p>三个节点都需要修改,修改成各自ip</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#[Member]</span>
<span class="nv">ETCD_DATA_DIR</span><span class="o">=</span><span class="s2">&#34;/var/lib/etcd/default.etcd&#34;</span>   <span class="c1">#etcd数据保存目录</span>
<span class="nv">ETCD_LISTEN_PEER_URLS</span><span class="o">=</span><span class="s2">&#34;http://0.0.0.0:2380&#34;</span>   <span class="c1">#集群内部通信使用的URL</span>
<span class="nv">ETCD_LISTEN_CLIENT_URLS</span><span class="o">=</span><span class="s2">&#34;http://0.0.0.0:2379&#34;</span>  <span class="c1">#供外部客户端使用的url</span>
<span class="nv">ETCD_NAME</span><span class="o">=</span><span class="s2">&#34;node1&#34;</span>  <span class="c1">#etcd实例名称</span>

<span class="c1">#[Clustering]</span>
<span class="nv">ETCD_INITIAL_ADVERTISE_PEER_URLS</span><span class="o">=</span><span class="s2">&#34;http://10.0.8.116:2380&#34;</span>  <span class="c1">#广播给集群内其他成员访问的URL</span>
<span class="nv">ETCD_ADVERTISE_CLIENT_URLS</span><span class="o">=</span><span class="s2">&#34;http://127.0.0.1:2379,http://10.0.8.166:2379&#34;</span> <span class="c1">#广播给外部客户端使用的url</span>
<span class="nv">ETCD_INITIAL_CLUSTER</span><span class="o">=</span><span class="s2">&#34;node1=http://10.0.8.116:2380,node2=http://10.0.8.159:2380,node3=http://10.0.8.122:2380&#34;</span> <span class="c1">#初始集群成员列表</span>
<span class="nv">ETCD_INITIAL_CLUSTER_TOKEN</span><span class="o">=</span><span class="s2">&#34;etcd-cluster&#34;</span> <span class="c1">#集群的名称</span>
<span class="nv">ETCD_INITIAL_CLUSTER_STATE</span><span class="o">=</span><span class="s2">&#34;new&#34;</span> <span class="c1">#初始集群状态，new为新建集群</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6启动服务">6.启动服务</h3>
<p>先启动两个从节点,然后启动主节点</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">systemctl daemon-reload
systemctl <span class="nb">enable</span> etcd
systemctl start etcd
systemctl status etcd
netstat -tunlp<span class="p">|</span>grep etcd
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>如果启动失败</p>
<p>1.检查配置文件</p>
<p>2.rm -rf /var/lib/etcd/*</p>
</blockquote>
<h3 id="7配置防火墙开放端口">7.配置防火墙开放端口</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">firewall-cmd --zone<span class="o">=</span>public --add-port<span class="o">=</span>2379/tcp --permanent
firewall-cmd --zone<span class="o">=</span>public --add-port<span class="o">=</span>2380/tcp --permanent
firewall-cmd --reload <span class="o">&amp;&amp;</span> firewall-cmd --list-ports
</code></pre></td></tr></table>
</div>
</div><h3 id="8集群维护">8.集群维护</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#查看集群列表</span>
etcdctl member list
<span class="c1">#查看集群健康状态</span>
etcdctl cluster-health
</code></pre></td></tr></table>
</div>
</div><h3 id="9etcd-api分为2和3版本">9.etcd api分为2和3版本</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#设置为3版本</span>
vim /etc/profile
【内容】
<span class="nb">export</span> <span class="nv">ETCDCTL_API</span><span class="o">=</span><span class="m">3</span>

<span class="nb">source</span> /etc/profile
</code></pre></td></tr></table>
</div>
</div><h3 id="10etcd操作数据">10.etcd操作数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#v2接口</span>
etcdctl <span class="nb">set</span> key value
etcdctl get key
etcdctl update key value
etcdctl rm key
etcdctl ls
etcdctl <span class="nb">help</span>

<span class="c1">#v3接口</span>
etcdctl put key value
etcdctl get key
etcdctl del key
etcdctl get / --prefix --keys-only
etcdctl <span class="nb">help</span>

tips:
key存在的方式和zookeeper类似，为 /路径/key
设置完之后，其他集群也可以查询到该值
如果dir和key不存在，该命令会创建对应的项
</code></pre></td></tr></table>
</div>
</div><h3 id="11测试集群是否成功">11.测试集群是否成功</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#node1</span>
etcdctl put /infinity/nodeinfo liang
etcdctl put /ceph/mdsinfo <span class="m">132312</span>
<span class="c1">#node2</span>
etcdctl get /infinity/nodeinfo
etcdctl get /ceph/mdsinfo
etcdctl put /ceph/mdsinfo <span class="m">11111</span>
<span class="c1">#node3</span>
etcdctl get /infinity/nodeinfo
etcdctl get /ceph/mdsinfo
etcdctl get / --prefix --keys-only
</code></pre></td></tr></table>
</div>
</div><h2 id="三应用中使用">三.应用中使用</h2>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License" target="_blank">Creative Commons Attribution-ShareAlike License</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a>
          <a href="/tags/middleware/">middleware</a>
          <a href="/tags/linux/">linux</a>
          <a href="/tags/centos7/">centos7</a>
          <a href="/tags/etcd/">etcd</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/middleware/confd&#43;etcd%E9%85%8D%E7%BD%AE%E5%90%8C%E6%AD%A5/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[etcd] confd&#43;etcd配置同步</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/dev_language/conf/toml%E9%85%8D%E7%BD%AE%E5%92%8C%E4%BD%BF%E7%94%A8/">
            <span class="next-text nav-default">[配置文件] toml配置和使用</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
